package cn.fl.comm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import cmm.comm.facade.framework.condition.SysDictionaryQueryCondition;
import cmm.mid.core.framework.condition.CacheRefreshDictionaryQC;
import cmm.mid.core.framework.constant.CacheRefreshConstant;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.comm.service.ICacheRefreshService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysTipConstant;
import cn.fl.system.vo.SysDictionaryVO;

/**
 * 数据字段管理控制层
 * @author FUJINGRUN
 *
 */
@Controller
@RequestMapping("/comm/dictionary")
public class DictionaryController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(DictionaryController.class);
	
	/**
	 * 本地缓存刷新服务
	 */
	@Autowired
	private ICacheRefreshService cacheRefreshService;
	

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		// 查询条件
		String sysCd = request.getParameter("searchSysCd");
		String cacheCodeGrpCdNm = request.getParameter("cacheCodeGrpCdNm");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysDictionaryQueryCondition qc = new SysDictionaryQueryCondition();
		if(StringUtils.isNotEmpty(sysCd)){
			qc.setSysCd(sysCd);
		}
		
		if (StringUtils.isNotEmpty(cacheCodeGrpCdNm)) {
			qc.setCodeGrpNm(cacheCodeGrpCdNm);
		}

		page.setCondition(qc);
		Pager pager = this.cacheRefreshService.searchGroupListPage(page);
		List<SysDictionaryVO> list = (List<SysDictionaryVO>) pager.getResultList();
		ListPageVO<SysDictionaryVO> listPage = new ListPageVO<SysDictionaryVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "初始化列表数据.DATA:" + com.alibaba.fastjson.JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 到数据字典列表展示页面
	 * @author JiLi 20170419
	 */
	@RequestMapping(value = "/toListInfoPage.do", method = { RequestMethod.GET})
	public ModelAndView toListInfoPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("comm/dictionary/dictionaryListInfo");
		List<KeyValueVO> sysCdList = this.cacheRefreshService.searchServiceSysCdList();
		if(sysCdList!=null){
			view.addObject("sysCdList", sysCdList);
		}
		BaseLogUtils.info(logger, "toListInfoPage", "跳转到数据字典列表展示页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 初始化数据字典列表展示页面
	 * @author JiLi 20170419
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initListInfo.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initListInfo(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		// 查询条件
		String sysCd = request.getParameter("searchSysCd");
		String codeGrpCd = request.getParameter("codeGrpCd");
		String codeGrpNm = request.getParameter("codeGrpNm");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysDictionaryQueryCondition qc = new SysDictionaryQueryCondition();
		if(StringUtils.isNotEmpty(sysCd)){
			qc.setSysCd(sysCd);
		}
		
		if (StringUtils.isNotEmpty(codeGrpCd)) {
			qc.setCodeGrpCd(codeGrpCd);
		}
		
		if (StringUtils.isNotEmpty(codeGrpNm)) {
			qc.setCodeGrpNm(codeGrpNm);
		}

		page.setCondition(qc);
		Pager pager = this.cacheRefreshService.searchDicListInfoPage(page);
		List<SysDictionaryVO> list = (List<SysDictionaryVO>) pager.getResultList();
		ListPageVO<SysDictionaryVO> listPage = new ListPageVO<SysDictionaryVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initListInfo", "初始化列表数据.DATA:" + com.alibaba.fastjson.JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
}
